package com.hsh.maoduo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hsh.maoduo.common.Result;
import com.hsh.maoduo.constant.common.DateConstants;
import com.hsh.maoduo.exception.MyException;
import com.hsh.maoduo.mapper.OutboundMapper;
import com.hsh.maoduo.pojo.dto.OutboundDTO;
import com.hsh.maoduo.pojo.entity.Outbound;
import com.hsh.maoduo.pojo.vo.OutboundVO;
import com.hsh.maoduo.service.GoodsService;
import com.hsh.maoduo.service.OutboundService;
import com.hsh.maoduo.util.DataUtil;
import com.hsh.maoduo.util.MyBeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author USER
 * @description 针对表【outbound】的数据库操作Service实现
 * @createDate 2023-12-23 16:35:07
 */
@Service
public class OutboundServiceImpl extends ServiceImpl<OutboundMapper, Outbound> implements OutboundService {

    @Resource
    private OutboundMapper outboundMapper;

    @Resource
    private GoodsService goodsService;

    @Override
    public List<OutboundDTO> getList(OutboundVO outboundVO) {
        outboundVO.setGoodsName(DataUtil.removeSpaceAndSymbols(outboundVO.getGoodsName()))
                .setShopName(DataUtil.removeSpaceAndSymbols(outboundVO.getShopName()))
                .setNo(DataUtil.removeSpaceAndSymbols(outboundVO.getNo()));
        List<OutboundDTO> outboundDTOList = outboundMapper.selectListByQuery(outboundVO);
        outboundDTOList.stream().forEach(OutboundDTO::init);
        return outboundDTOList;
    }

    @Override
    public Result<Integer> addList(List<OutboundVO> outboundVOList) {
        int insertNum = 0;
        Date now = new Date();
        for (OutboundVO outboundVO : outboundVOList) {
            outboundVO.setNo(DateConstants.PURE_DATETIME_MS_FORMAT.format(now));
            try {
                insertNum += addOne(outboundVO);
            } catch (MyException e) {
                log.error("添加失败,id为：" + outboundVO.getId());
            }
        }
        return Result.create(insertNum);
    }

    @Override
    @Transactional
    public int addOne(OutboundVO outboundVO) {
        int insert;
        int updateNum = -1 * (outboundVO.getNum() == null ? 0 : outboundVO.getNum());
        int updatedGoodsNum = goodsService.updateGoodsNum(outboundVO.getGoodsId(), updateNum);
        if (updatedGoodsNum == 0) {
            return 0;
        }
        if (outboundVO.getCreateTime()==null){
            outboundVO.setCreateTime(new Date());
        }
        insert = outboundMapper.insert(MyBeanUtils.copy(outboundVO, Outbound.class));
        return insert;
    }

    @Override
    @Transactional
    public Result<Integer> update(OutboundVO outboundVO) {
        Outbound oldOutbound = outboundMapper.selectById(outboundVO.getId());
        int updateGoodsNum = outboundVO.getNum() - oldOutbound.getNum();
        goodsService.updateGoodsNum(outboundVO.getGoodsId(), -1 * updateGoodsNum);

        int updateNum = outboundMapper.updateById(MyBeanUtils.copy(outboundVO, Outbound.class));
        return Result.create(updateNum);
    }

    @Override
    @Transactional
    public Result<Integer> delete(int id) {
        Outbound outbound = outboundMapper.selectById(id);
        int deleteNum = outboundMapper.updateById(new Outbound().setId(id).setIsDelete(1));
        goodsService.updateGoodsNum(outbound.getGoodsId(), outbound.getNum());
        return Result.create(deleteNum);
    }


    @Override
    public List<OutboundDTO> getSumGroupByGoodId(OutboundVO outboundVO) {
        QueryWrapper wrapper = new QueryWrapper<>(MyBeanUtils.copy(outboundVO, Outbound.class));
        wrapper.select("goods_id", "SUM(num) num");
        if (!Objects.isNull(outboundVO.getStartDateTime())) {
            wrapper.ge("create_time", outboundVO.getStartDateTime());
        }
        if (!Objects.isNull(outboundVO.getEndDateTime())) {
            wrapper.le("create_time", outboundVO.getEndDateTime());
        }
        wrapper.groupBy("goods_id");
        return outboundMapper.selectList(wrapper);
    }

    @Override
    public Result batchVerified(Integer[] ids) {
        Outbound outbound = new Outbound().setIsVerified(1);
        UpdateWrapper wrapper = new UpdateWrapper();
        wrapper.eq("is_delete", 0);
        wrapper.in(!Objects.isNull(ids), "id", ids);
        int update = outboundMapper.update(outbound, wrapper);
        return Result.create(update);
    }

    @Override
    public Result batchDelete(Integer[] ids) {
        int update = 0;
        for (Integer id : ids) {
            update += delete(id).getData();
        }
        return Result.create(update);
    }

}




